package cn.yunhe.controller;

import cn.yunhe.model.Role;
import cn.yunhe.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Map;

/**
 * Created by zs on 2017/12/15.
 */
@Controller
@SessionAttributes(value = {"names"}, types = Role.class)
@RequestMapping("/user")
public class UserController {

    @RequestMapping("/login")
    public String login(User user, HttpSession session) {
        session.setAttribute("username", user.getUsername());
        session.setAttribute("password", user.getPassword());
        return "forward:list.do";    //redirect   forward
    }

    @RequestMapping("/list")
    public ModelAndView list() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("succ");
        return mv;
    }

    @RequestMapping("/sa")
    public String SessionAttr(Map<String, Object> map) {
        map.put("names", Arrays.asList("aa", "bb", "ccc"));
        map.put("age", "12");
        Role role = new Role();
        role.setRoleName("administrator");
        map.put("role", role);
        return "index";
    }

    @RequestMapping("/getRole/{id}")
    public ModelAndView getPathVar(@PathVariable("id") Integer id) {
        ModelAndView mv = new ModelAndView();
        System.out.println("id==========" + id);
        mv.setViewName("index");
        return mv;
    }


}
